package com.fl.nj.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;

/**
 * 字符编码过滤器配置
 * 确保所有HTTP响应都使用UTF-8编码，解决中文乱码问题
 */
@Configuration
public class CharacterEncodingFilterConfig {

    @Bean
    public FilterRegistrationBean<CharacterEncodingFilter> characterEncodingFilterRegistration() {
        // 创建字符编码过滤器
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        
        // 设置强制使用UTF-8编码
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        
        // 注册过滤器
        FilterRegistrationBean<CharacterEncodingFilter> registrationBean = 
            new FilterRegistrationBean<>(characterEncodingFilter);
        
        // 设置过滤器名称
        registrationBean.setName("characterEncodingFilter");
        
        // 设置过滤器的URL模式，匹配所有请求
        registrationBean.addUrlPatterns("/*");
        
        // 设置过滤器的执行顺序，数字越小优先级越高
        registrationBean.setOrder(1);
        
        return registrationBean;
    }
}